//
//  NSHash.m
//  erhuo
//
//  Created by Jingru Nie on 15/1/5.
//  Copyright (c) 2015年 集易（北京）科技有限公司. All rights reserved.
//

#import "NSHash.h"
#import <CommonCrypto/CommonCrypto.h>

@implementation NSHash

+ (NSString *)MD5:(NSString *)origin {
    const char *cStr = [origin UTF8String];
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    CC_MD5(cStr, strlen(cStr), digest);
    
    NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
    {
        [output appendFormat:@"%02x", digest[i]];
    }
    return output;
}

+ (NSString *)RandomString:(unsigned)count {
    NSMutableString *result = [NSMutableString string];
    NSString *source = @"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    NSInteger leng = [source length];
    for (int i = 0; i < count; i++) {
        NSInteger index = arc4random() % leng;
        [result appendString:[source substringWithRange:NSMakeRange(index, 1)]];
    }
    return result;
}

@end
